<template>
  <div id="box">
    <div class="formBox">
      <el-container>
        <el-header>
          <el-link type="info" @click="denglu" class="a">| 登录</el-link>
          <el-link type="info" @click="zhuce" class="a">注册 |</el-link>
        </el-header>
        <el-main>
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item prop="phone">
              <el-input
                placeholder="手机号码"
                type="text"
                v-model="ruleForm.phone"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item prop="password">
              <el-input
                placeholder="原密码"
                type="password"
                v-model="ruleForm.password"
                autocomplete="off"
              ></el-input>
            </el-form-item> -->
            <el-form-item prop="newPass">
              <el-input
                placeholder="新密码"
                type="password"
                v-model="ruleForm.newPass"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item prop="checkPass">
              <el-input
                placeholder="确认新密码"
                type="password"
                v-model="ruleForm.checkPass"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >提交</el-button
              >
              <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
            </el-form-item>
          </el-form>
        </el-main>
        <el-footer><h1>底部</h1></el-footer>
      </el-container>
    </div>
  </div>
</template>

<script>
export default {
  name: "xiugai",
  data() {
    const validatePhone = (rule, value, callback) => {
      //正则表达式进行验证手机号
      if (!/^1[35789]\d{9}$/.test(value)) {
        callback(new Error("手机号格式不正确"));
      }
      callback();
    };
    // var validatePassword = (rule, value, callback) => {
    //   if (value === "") {
    //     callback(new Error("请输入原密码"));
    //   } else {
    //     if (this.ruleForm.checkPass !== "") {
    //       this.$refs.ruleForm.validateField("checkPass");
    //     }
    //     callback();
    //   }
    // };
    var validateNewPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validateCheckPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入新密码"));
      } else if (value !== this.ruleForm.newPass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        phone: "",
        // password: "",
        newPass: "",
        checkPass: "",
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { min: 11, max: 11, message: "长度在11个字符", trigger: "blur" },
          { required: true, trigger: "blur", validator: validatePhone },
        ],
        // password: [{ validator: validatePassword, trigger: "blur" }],
        newPass: [{ validator: validateNewPass, trigger: "blur" }],
        checkPass: [{ validator: validateCheckPass, trigger: "blur" }],
      },
    };
  },
  methods: {
    // 登录页
    denglu() {
      this.$router.replace({ path: "/login" });
    },
    // 注册页
    zhuce() {
      this.$router.replace({ path: "/index" });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 验证账号是否存在
          this.$http
            .get("http://localhost:8082/chaxun", { params: this.ruleForm })
            .then((res) => {
              console.log(res.data.length);
              console.log(this.ruleForm);
              if (res.data.length == 1) {
                if (valid) {
                  // 账号存在后修改密码
                  this.$http
                    .get("http://localhost:8082/xiugai", {
                      params: {
                        phone: this.ruleForm.phone,
                        newPass: this.ruleForm.newPass,
                      },
                    })
                    .then((res) => {
                      console.log(res.data);
                      alert("密码修改成功!点击确认跳转到登录页");
                      this.$router.replace({ path: "/login" });
                    });
                } else {
                  // console.log("error submit!!");
                }
              } else {
                return alert("手机号不存在请确认后输入!");
              }
            });
        } else {
          // console.log("error submit!!");
        }
      });
    },
    // resetForm(formName) {
    // },
  },
};
</script>
<style scoped>
#box {
  text-align: center;
}
.formBox {
  width: 600px;
  height: 100%;
  margin: 0 auto;
}
.a {
  float: right;
}
</style>
